import com.sun.org.apache.bcel.internal.generic.StackInstruction;

import java.util.Stack;

class MinStack {
    private Stack<Integer> stack;
    private Stack<Integer> minStack;

    public MinStack() { //用于初始化栈
        stack = new Stack<>();
        minStack = new Stack<>();
    }
    
    public void push(int val) {
        stack.push(val);
        if(minStack.empty()){
            minStack.push(val);
        }else {
            int x = minStack.peek();
            if(val <= x){
                minStack.push(val);
            }
        }
    }
    //删除堆栈顶部的元素。对普通栈和最小栈都要执行
    public void pop() {
        if(!stack.empty()){
            int x = stack.pop();
            if(x == minStack.peek()){
                minStack.pop();
            }
        }
    }
    //获取堆栈顶部的元素,只针对普通栈来说
    public int top() {
        if(!stack.empty()){
            return stack.peek();
        }
        return -1; //不能抛出异常
    }
    
    public int getMin() {
        if(!minStack.empty()){
            return minStack.peek();
        }
        return -1;
    }
}
